package com.example.demo.service.impl;

import com.example.demo.dao.UserMapper;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName UserServiceImpl
 * @Description: TODO
 * @Author ChenYuan
 * @Date 2020/11/18
 * @Version V1.0
 **/
@Service
@Slf4j
public class UserServiceImpl implements UserService {
    private static final Logger logger = LoggerFactory.getLogger(UserService.class);

    @Resource
    private UserMapper userDao;

    @Override
    public List<User> getUser(){
        List<User> userList = userDao.getUser();
        log.info("查询出来的用户信息，{}",userList.toString());
        log.warn("测试，{}","zhansan");
        log.debug("测试，{}","lisi");
        log.error("测试，{}","shiabi");
        return userList;
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class},propagation = Propagation.REQUIRED)
    public void addUser(boolean isEx) {
//        UserService proxy = (UserService)AopContext.currentProxy();
//        proxy.realAdd(isEx);
        realAdd(isEx);
    }

    @Override
    public void realAdd(boolean isEx) {
        User user = new User();
//        user.setId(12);
        user.setPassword("1234567889");
        user.setUsername("testTranction");
        user.setSalt("哈哈");
        userDao.insert(user);
        if (isEx){
            double a = 1/0;
        }
    }
}
